﻿<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 
			 xmlns:XDCCManager_Converters="clr-namespace:XDCCManager.Converters" x:Name="userControl" 
			 xmlns:Servers="clr-namespace:XDCCManager.Model.Servers"
			 xmlns:Popups="clr-namespace:XDCCManager.Popups"
			 x:Class="XDCCManager.Popups.ServerEditor" 
             mc:Ignorable="d" 
             DataContextChanged="userControl_DataContextChanged">
	<UserControl.DataContext>
		<Servers:Server Network="The network" Nick="Your nick" Password="Some pass" BotName="The Bot">
			<Servers:Server.Addresses>
				<Servers:ServerAddress Hostname="Some host.net" Port="6667" Ssl="False" />
				<Servers:ServerAddress Hostname="Some host2.net" Port="6600" Ssl="True" />
				<Servers:ServerAddress Hostname="Some host3.net" Port="6668" Ssl="False" />
			</Servers:Server.Addresses>
			<Servers:Server.UploadService>
				<Servers:FtpUploadService Host="ftphost" Port="21" Username="ftpuser" Password="ftppass"/>
			</Servers:Server.UploadService>
		</Servers:Server>
	</UserControl.DataContext>
	<UserControl.Resources>
		<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
		<XDCCManager_Converters:BooleanToReverseVisibilityConverter x:Key="BooleanToReverseVisibilityConverter"/>
		<DataTemplate x:Key="ServerAddressHostnameTemplate">
			<TextBox Text="{Binding Hostname, Mode=TwoWay}" />
		</DataTemplate>
		<DataTemplate x:Key="ServerAddressPortTemplate">
			<TextBox Text="{Binding Port, Mode=TwoWay}" TextAlignment="Right" MaxLength="5" />
		</DataTemplate>
		<DataTemplate x:Key="ServerAddressSslTemplate">
			<CheckBox IsChecked="{Binding Ssl, Mode=TwoWay}" VerticalAlignment="Center" />
		</DataTemplate>
		<DataTemplate x:Key="RemoveTemplate">
			<Button Content="Remove" Click="Remove_Click" />
		</DataTemplate>
		<Style TargetType="{x:Type ListViewItem}">
			<Setter Property="HorizontalContentAlignment" Value="Stretch" />
			<Setter Property="VerticalContentAlignment" Value="Stretch" />
			<Setter Property="Background" Value="#808080" />
			<Setter Property="Margin" Value="0,0,0,0" />
		</Style>
		<Style TargetType="{x:Type GridViewHeaderRowPresenter}">
			<Setter Property="Margin" Value="0,0,0,0" />
		</Style>
		<Style TargetType="{x:Type GridViewColumnHeader}">
			<Setter Property="Margin" Value="0,0,0,0" />
		</Style>
		<DataTemplate x:Key="FtpDataTemplate">
			<Grid>
				<Grid.ColumnDefinitions>
					<ColumnDefinition Width="Auto"/>
					<ColumnDefinition/>
				</Grid.ColumnDefinitions>
				<Grid.RowDefinitions>
					<RowDefinition Height="Auto"/>
					<RowDefinition Height="Auto"/>
					<RowDefinition Height="Auto"/>
					<RowDefinition Height="Auto"/>
				</Grid.RowDefinitions>
				<Label Content="Host"/>
				<TextBox Text="{Binding Host, Mode=TwoWay}" Grid.Column="1"/>
				<Label Content="Port" Grid.Row="1"/>
				<TextBox Text="{Binding Port, Mode=TwoWay}" Grid.Column="1" Grid.Row="1" Width="40" MaxLength="5" TextAlignment="Right" HorizontalAlignment="Left"/>
				<Label Content="Username" Grid.Row="2"/>
				<TextBox Text="{Binding Username, Mode=TwoWay}" Grid.Column="1" Grid.Row="2"/>
				<Label Content="Password" Grid.Row="3"/>
				<TextBox Text="{Binding Password, Mode=TwoWay}" Grid.Column="1" Grid.Row="3"/>
			</Grid>
		</DataTemplate>
		<DataTemplate x:Key="NoneDataTemplate">
			<TextBlock TextWrapping="Wrap" Text="No settings"/>
		</DataTemplate>
		<Popups:UploadServiceTemplateSelector x:Key="uploadServiceTemplateSelector">
			<Popups:UploadServiceTemplateSelector.Definitions>
				<Popups:UploadServiceTemplateDefinition Kind="None" Template="{StaticResource NoneDataTemplate}"/>
				<Popups:UploadServiceTemplateDefinition Kind="Ftp" Type="{x:Type Servers:FtpUploadService}" Template="{StaticResource FtpDataTemplate}"/>
			</Popups:UploadServiceTemplateSelector.Definitions>
		</Popups:UploadServiceTemplateSelector>
	</UserControl.Resources>
	<StackPanel Background="{Binding Background, ElementName=userControl}">
		<GroupBox>
			<GroupBox.Header>
				<StackPanel Orientation="Horizontal">
					<TextBlock TextWrapping="Wrap" Text="New server" Visibility="{Binding ShowRemove, Converter={StaticResource BooleanToReverseVisibilityConverter}, ElementName=userControl}"/>
					<TextBlock TextWrapping="Wrap" Text="Edit server" Visibility="{Binding ShowRemove, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=userControl}"/>
				</StackPanel>
			</GroupBox.Header>
			<Grid x:Name="LayoutRoot">
				<Grid.RowDefinitions>
					<RowDefinition Height="Auto"/>
					<RowDefinition Height="Auto"/>
					<RowDefinition Height="Auto"/>
					<RowDefinition Height="Auto"/>
					<RowDefinition Height="Auto"/>
					<RowDefinition Height="Auto"/>
					<RowDefinition Height="Auto"/>
					<RowDefinition Height="Auto"/>
				</Grid.RowDefinitions>
				<Grid.ColumnDefinitions>
					<ColumnDefinition Width="Auto"/>
					<ColumnDefinition/>
				</Grid.ColumnDefinitions>
				<Label Content="Network"/>
				<TextBox TextWrapping="Wrap" Text="{Binding Network, Mode=TwoWay}" Grid.Column="1"/>
				<Label Content="Addresses" Grid.Row="1"/>
				<Button Content="Add" d:LayoutOverrides="Height" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" Click="Add_Click"/>
				<ListView Grid.ColumnSpan="2" Grid.Row="2" ItemsSource="{Binding Addresses}" Height="75" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionMode="Single">
					<ListView.View>
						<GridView AllowsColumnReorder="False">
							<GridViewColumn Header="Hostname" CellTemplate="{DynamicResource ServerAddressHostnameTemplate}" Width="130"/>
							<GridViewColumn Header="Port" CellTemplate="{DynamicResource ServerAddressPortTemplate}" Width="55"/>
							<GridViewColumn Header="SSL" CellTemplate="{DynamicResource ServerAddressSslTemplate}" Width="30"/>
							<GridViewColumn CellTemplate="{DynamicResource RemoveTemplate}" Header="Remove" Width="65"/>
						</GridView>
					</ListView.View>
				</ListView>
				<Label Content="Nick" Grid.Row="3"/>
				<TextBox TextWrapping="Wrap" Text="{Binding Nick, Mode=TwoWay}" Grid.Column="1" Grid.Row="3"/>
				<Label Content="Bot name" Grid.Row="4"/>
				<TextBox TextWrapping="Wrap" Text="{Binding BotName, Mode=TwoWay}" Grid.Column="1" Grid.Row="4"/>
				<Label Content="Admin password" Grid.Row="5"/>
				<PasswordBox x:Name="password" Grid.Column="1" Grid.Row="5" PasswordChanged="PasswordBox_PasswordChanged"/>
				<Label Content="Upload service kind" Grid.Row="6"/>
				<ComboBox x:Name="comboTypes" Grid.Row="6" Grid.Column="1" VerticalAlignment="Top" SelectedItem="{Binding UploadServiceKind, Mode=TwoWay}">
					<ComboBox.Items>
						<Servers:UploadServiceKind>None</Servers:UploadServiceKind>
						<Servers:UploadServiceKind>FTP</Servers:UploadServiceKind>
					</ComboBox.Items>
				</ComboBox>
				<GroupBox Grid.ColumnSpan="2" Grid.Row="7">
					<GroupBox.Header>
						<TextBlock Text="Upload service"/>
					</GroupBox.Header>
					<ListBox HorizontalContentAlignment="Stretch" Background="{x:Null}" BorderBrush="{x:Null}">
						<ListBoxItem Content="{Binding UploadService}" ContentTemplateSelector="{StaticResource uploadServiceTemplateSelector}"/>
					</ListBox>
				</GroupBox>
			</Grid>
		</GroupBox>
		<UniformGrid HorizontalAlignment="Right" Rows="1">
			<Button Content="Remove" Click="ItemRemove_Click" Visibility="{Binding ShowRemove, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=userControl}"/>
			<Button Content="Cancel" Click="Cancel_Click"/>
			<Button Content="OK" Click="Ok_Click"/>
		</UniformGrid>
	</StackPanel>
</UserControl>
